package com.asm.DBloader;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ASM_FOrdHedDS {
	
	private SQLiteDatabase dB;
	private ASM_DB dbHelper;
	Context context;
	
	
	public ASM_FOrdHedDS(Context context) {
		this.context = context;
		dbHelper  = new ASM_DB(context);
		
	}
	
	public void open() throws SQLException
	{
		dB = dbHelper.getWritableDatabase();
	}
	public int Create(ASM_FOrdHed det) {
		
		int count =0;
		
		if(dB == null){
			open();
		}else if(!dB.isOpen()){
			open();
		}
		
		
		ContentValues values = new ContentValues();
		values.put(ASM_DB.asmHED_RefNo, det.getAsmHED_RefNo());
		values.put(ASM_DB.asmHED_TxnDate, det.getAsmHED_TxnDate().toString().trim());
		values.put(ASM_DB.asmHED_ManuRef,det.getAsmHED_ManuRef());
		values.put(ASM_DB.asmHED_DelvDate, det.getAsmHED_DelvDate());
		values.put(ASM_DB.asmHED_DealCode, det.getAsmHED_DealCode());
		values.put(ASM_DB.asmHED_RepCode,det.getAsmHED_RepCode());
		values.put(ASM_DB.asmHED_AreaCode,det.getAsmHED_AreaCode());
		values.put(ASM_DB.asmHED_RouteCode, det.getAsmHED_RouteCode());
		values.put(ASM_DB.asmHED_DebCode,det.getAsmHED_DebCode());
		values.put(ASM_DB.asmHED_LocCode, det.getAsmHED_LocCode());
		values.put(ASM_DB.asmHED_Remarks, det.getAsmHED_Remarks());
		values.put(ASM_DB.asmHED_CostCode,det.getAsmHED_CostCode());
		values.put(ASM_DB.asmHED_CurCode, det.getAsmHED_CurCode());
		values.put(ASM_DB.asmHED_CurRate, det.getAsmHED_CurRate());
		values.put(ASM_DB.asmHED_TxnType,det.getAsmHED_TxnType());
		values.put(ASM_DB.asmHED_InvAmt, det.getAsmHED_InvAmt());
		values.put(ASM_DB.asmHED_OutDis, det.getAsmHED_OutDis());
		values.put(ASM_DB.asmHED_InvDis,det.getAsmHED_InvDis());
		values.put(ASM_DB.asmHED_TotMkrAmt, det.getAsmHED_TotMkrAmt());
		values.put(ASM_DB.asmHED_UnOvAmt, det.getAsmHED_UnOvAmt());
		values.put(ASM_DB.asmHED_TotalDis,det.getAsmHED_TotalDis());
		values.put(ASM_DB.asmHED_TotalItmDis,det.getAsmHED_TotalItmDis());
		values.put(ASM_DB.asmHED_TotalAmt,det.getAsmHED_TotalAmt());
		values.put(ASM_DB.asmHED_AddUser,det.getAsmHED_AddUser());
		values.put(ASM_DB.asmHED_AddDate, det.getAsmHED_AddDate());
		values.put(ASM_DB.asmHED_AddMach, det.getAsmHED_AddMach());
		values.put(ASM_DB.asmHED_InvFlg,det.getAsmHED_InvFlg());
		values.put(ASM_DB.asmHED_prtcopy, det.getAsmHED_prtcopy());
		values.put(ASM_DB.asmHED_RecordId, det.getAsmHED_RecordId());
		values.put(ASM_DB.asmHED_TranBatch, det.getAsmHED_TranBatch());
		values.put(ASM_DB.asmHED_OutDisVal,det.getAsmHED_OutDisVal());
		values.put(ASM_DB.asmHED_Longitude, det.getAsmHED_Longitude());
		values.put(ASM_DB.asmHED_Latitude, det.getAsmHED_Latitude());
		values.put(ASM_DB.asmHED_startTimeSO,det.getAsmHED_startTimeSO());
		values.put(ASM_DB.asmHED_endTimeSO, det.getAsmHED_endTimeSO());
		values.put(ASM_DB.asmHED_address, det.getAsmHED_address());
		values.put(ASM_DB.asmHED_clssts, det.getAsmHED_clssts());
		values.put(ASM_DB.asmHED_refno2,det.getAsmHED_refno2());
		values.put(ASM_DB.asmHED_PayType,det.getAsmHED_PayType());
		values.put(ASM_DB.asmHED_Skutype,det.getAsmHED_Skutype());
		values.put(ASM_DB.asmHED_DiffDate,det.getAsmHED_DiffDate());
		
		count = (int) dB.insert(ASM_DB.ASM_FORDHED, null, values);

		dB.close();
		return count;
		
	}
	
	public ArrayList<String> getsolisting(String startdate,String enddate,String Repcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		String selectQuery = "select a.RefNo,a.TxnDate,b.DebName,a.address,a.startTimeSO,a.TotalAmt from FOrdHed a,fDebtor b where a.DebCode = b.DebCode and strftime('%Y-%m-%d',a.TxnDate) BETWEEN '"+startdate+"' and '"+enddate+"' and adduser = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
			String RefNo = cursor.getString(0);
//			String TxnDate = cursor.getString(1);
//			String DebName = cursor.getString(2);
//			String address = cursor.getString(3);
//			String startTimeSO = cursor.getString(4);
//			String TotalAmt = cursor.getString(5);
//		
			Details.add(RefNo);
//			Details.add(TxnDate);
//			Details.add(DebName);
//			Details.add(address);
//			Details.add(startTimeSO);
//			Details.add(TotalAmt);
			

			
			
			
			
		}
		
	
		return Details;
	}
	
	public ArrayList<String> getsolisting1(String startdate,String enddate,String Repcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		String selectQuery = "select a.RefNo,a.TxnDate,b.DebName,a.address,a.startTimeSO,a.TotalAmt from FOrdHed a,fDebtor b where a.DebCode = b.DebCode and strftime('%Y-%m-%d',a.TxnDate) BETWEEN '"+startdate+"' and '"+enddate+"' and adduser = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
//			String RefNo = cursor.getString(0);
			String TxnDate = cursor.getString(1);
//			String DebName = cursor.getString(2);
//			String address = cursor.getString(3);
//			String startTimeSO = cursor.getString(4);
//			String TotalAmt = cursor.getString(5);
//		
//			Details.add(RefNo);
			Details.add(TxnDate);
//			Details.add(DebName);
//			Details.add(address);
//			Details.add(startTimeSO);
//			Details.add(TotalAmt);
			

			
			
			
			
		}
		
	
		return Details;
	}
	
	public ArrayList<String> getsolisting2(String startdate,String enddate,String Repcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		String selectQuery = "select a.RefNo,a.TxnDate,b.DebName,a.address,a.startTimeSO,a.TotalAmt from FOrdHed a,fDebtor b where a.DebCode = b.DebCode and strftime('%Y-%m-%d',a.TxnDate) BETWEEN '"+startdate+"' and '"+enddate+"' and adduser = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
//			String RefNo = cursor.getString(0);
//			String TxnDate = cursor.getString(1);
			String DebName = cursor.getString(2);
//			String address = cursor.getString(3);
//			String startTimeSO = cursor.getString(4);
//			String TotalAmt = cursor.getString(5);
//		
//			Details.add(RefNo);
//			Details.add(TxnDate);
			Details.add(DebName);
//			Details.add(address);
//			Details.add(startTimeSO);
//			Details.add(TotalAmt);
			

			
			
			
			
		}
		
	
		return Details;
	}
	
	
	public ArrayList<String> getsolisting3(String startdate,String enddate,String Repcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		String selectQuery ="select a.RefNo,a.TxnDate,b.DebName,a.address,a.startTimeSO,a.TotalAmt from FOrdHed a,fDebtor b where a.DebCode = b.DebCode and strftime('%Y-%m-%d',a.TxnDate) BETWEEN '"+startdate+"' and '"+enddate+"' and adduser = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
//			String RefNo = cursor.getString(0);
//			String TxnDate = cursor.getString(1);
//			String DebName = cursor.getString(2);
			String address = cursor.getString(3);
//			String startTimeSO = cursor.getString(4);
//			String TotalAmt = cursor.getString(5);
////		
//			Details.add(RefNo);
//			Details.add(TxnDate);
//			Details.add(DebName);
			Details.add(address);
//			Details.add(startTimeSO);
//			Details.add(TotalAmt);
			

			
			
			
			
		}
		
	
		return Details;
	}
	
	public ArrayList<String> getsolisting4(String startdate,String enddate,String Repcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		String selectQuery = "select a.RefNo,a.TxnDate,b.DebName,a.address,a.startTimeSO,a.TotalAmt from FOrdHed a,fDebtor b where a.DebCode = b.DebCode and strftime('%Y-%m-%d',a.TxnDate) BETWEEN '"+startdate+"' and '"+enddate+"' and adduser = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
		
//			String RefNo = cursor.getString(0);
//			String TxnDate = cursor.getString(1);
//			String DebName = cursor.getString(2);
//			String address = cursor.getString(3);
			String startTimeSO = cursor.getString(4);
//			String TotalAmt = cursor.getString(5);
////		
//			Details.add(RefNo);
//			Details.add(TxnDate);
//			Details.add(DebName);
//			Details.add(address);
			Details.add(startTimeSO);
//			Details.add(TotalAmt);
			

			
			
			
			
		}
		
	
		return Details;
	}
	
	public ArrayList<String> getsolisting5(String startdate,String enddate,String Repcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		String selectQuery = "select a.RefNo,a.TxnDate,b.DebName,a.address,a.startTimeSO,a.TotalAmt from FOrdHed a,fDebtor b where a.DebCode = b.DebCode and strftime('%Y-%m-%d',a.TxnDate) BETWEEN '"+startdate+"' and '"+enddate+"' and adduser = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			BigDecimal decimal = new BigDecimal(cursor.getString(5));
			decimal = decimal.setScale(2, RoundingMode.HALF_UP);
			
			Details.add(decimal.toString());

			
		}
		
	
		return Details;
	}
	
	
	public ArrayList<String> gettotalsum(String startdate,String enddate,String Repcode) 
	{
		if (dB == null) 
		{
			open();
		} else if (!dB.isOpen()) 
		{
			open();
		}
		ArrayList<String> Details = new ArrayList<String>();
		String selectQuery = "select sum(a.TotalAmt) from FOrdHed a,fDebtor b where a.DebCode = b.DebCode and strftime('%Y-%m-%d',a.TxnDate) BETWEEN '"+startdate+"' and '"+enddate+"' and adduser = '"+Repcode+"'";
		
		Cursor cursor = null;
		cursor = dB.rawQuery(selectQuery, null);
		
		while(cursor.moveToNext())
		{
			BigDecimal decimal = new BigDecimal(cursor.getString(0));
			decimal = decimal.setScale(2, RoundingMode.HALF_UP);
			
			Details.add(decimal.toString());

			
		}
		
	
		return Details;
	}
}
